


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @author mysens
 * @date 17-12-25 下午3:36
 */

public class RedisConfig {
    private String password="bevolredis#@";
    private String host="test-api.bevol.cn";
    private int port=6399;

    private JedisPool pool;

    //可用连接实例的最大数目，默认值为8；
    //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
    private static int MAX_ACTIVE = 1024;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值8。
    //private static int MAX_IDLE = 200;
    private static int MAX_IDLE = 20;
    //等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static boolean TEST_ON_BORROW = true;

   /* @Bean
    public Jedis init(){
        {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            pool = new JedisPool(config, host, port, TIMEOUT,password);

            try {
                return pool.getResource();
            }catch(JedisConnectionException e) {
                System.out.println("redis没有设置密码");
                pool = new JedisPool(config, host, port, TIMEOUT);
                //System.out.println("再次获取资源："+pool.getResource());
                e.printStackTrace();
            }

        }
        return null;
    }*/
    public RedisConfig(){
        {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            pool = new JedisPool(config, host, port, TIMEOUT,password);
        }
    }
    public Jedis createJedis(){
        Jedis jedis=  pool.getResource();
        return jedis;
    }
   /* public Jedis createJedis(){
        Jedis jedis=new Jedis("test-api.bevol.cn",6399);
        jedis.auth("bevolredis#@");
        return jedis;
    }*/
    public static Jedis getJedis(){
        Jedis jedis= new Jedis("test-api.bevol.cn",6399);
        jedis.auth("bevolredis#@");
        return jedis;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
